<template>
	<view>
		<view>
			<view>
				<view class='header'>
					<text>智慧园区</text>
				</view>
				<view v-if="!userInfo ">
					<view class='content'>
						<view>申请获取以下权限</view>
						<text>获得你微信昵称和头像</text>
					</view>
					
					<button class='bottom' type='primary' @click="getUserInfo">
						授权头像
					</button>
				</view>
				<view v-else>
					<view class='content'>
						<view>申请获取以下权限</view>
						<text>获得你微信绑定的手机号</text>
					</view>
					
					<button class='bottom' type='primary' open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
						手机号登录
					</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
  import { config } from '../../api/config'
	import * as api from '../../api/methods'
	var that;
	export default {
		data() {
			return {
				appCode: "",
				userInfo: {}
			}
		},
		onLoad() {
      that = this
			this.login()
		},
		
		methods: {
			login() {
				uni.login({
					provider: 'weixin',
					success: async (loginRes) => {
						that.appCode = loginRes.code;
					},
					fail: (error) => {
						console.log('error', error)
					}
				});
			},
			getUserInfo() {
				uni.getUserProfile({ 
					lang: 'zh_CN',
					desc:'获取用户信息',
					success: userInfo => {
						console.log('userInfo', userInfo);
						if (userInfo.errMsg == 'getUserProfile:ok') {
							that.userInfo = userInfo.userInfo
						} else {
						}
					 },
					fail:err=>{
						console.log(err,'err')
					}
				});
			},
			toParams(param) {
			    var result = ""
			    for (let name in param) {
			        if (typeof param[name] != 'function') {
			            result += "&" + name + "=" + encodeURI(param[name]);
			        }
			    }
			    return result.substring(1)
			},
			getPhoneNumber(e) {
				console.log("phone",e)
				wx.checkSession({
					success: async function() {
						var ency = e.detail.encryptedData; //.replace('+','%2B')
						var iv = e.detail.iv;//.replace('+','%2B')
						var code = that.appCode;
						var nickName = that.userInfo.nickName;
						var headImg = that.userInfo.avatarUrl;
						if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
							uni.showToast({
								title: '请允许获取你的手机号码，否则部分功能无法使用',
								mask: true,
								icon: 'none'
							})
						} else { //同意授权
							let canshu = 'orgId='+ config.orgId +'&channelCode=10010&encryptedData='+ency +'&iv='+iv+'&code='+code //+'&headImg='+headImg+'&nickName='+encodeURIComponent(nickName)
							canshu = canshu.replace(' ','+')
							// let url = '/pre?'+ canshu
							// let pages = getCurrentPages(); //获取当前页面js里面的pages里的所有信息。
							// let prevPage = pages[ pages.length - 2 ];
							// prevPage.$vm.setUrl(url)
							// uni.navigateBack()
						}
					},
					fail: () => {
						console.log("登录已超时，请重新登录");
						this.login(); //重新登录
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	.header {
		padding: 100rpx 0 60rpx 0;
		text-align: center;
		width: 100%;
		height: 300rpx;
		line-height: 300rpx;
		font-size: 50rpx;
	}

	.content {
		margin-left: 60rpx;
		margin-bottom: 50rpx;
	}
	
	.content view{
		font-size: 38rpx;
	}

	.content text {
		display: block;
		color: #9d9d9d;
		margin-top: 36rpx;
	}

	.bottom {
		border-radius: 80rpx;
		margin: 0 50rpx;
		font-size: 35rpx;
	}

	.bottom:first-child {
		margin-top: 50rpx;
	}

	.view_input {
		margin: 0 50rpx;
		background-color: white;
		padding: 10px;
		height: 1rem;
		line-height: 1rem;
	}
	.youke{
		margin-top: 20px;
	}
</style>
